// pages/listingGroup/listingGroup.js
var kLine = require('../../component/kLine/kLine.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupId: '',
    groupName: '',
    groupCode: '',
    current: {},
    listings: [],
    quoteData: {
      canvasIndex: 1
    },
    hidden: false,
    kline: 'day',
    canvasId: 2,//默认为日线画布
    period: 100,//默认为日线格式
    refresh: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      groupId: options.groupId,
      groupName: options.name,
      groupCode: options.code
    })
    this.kLineInit();
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      refresh: true
    })
    this.getCurrent();
    this.getListings();
    wx.setNavigationBarTitle({
      title: `${this.data.groupName} (${this.data.groupCode})`
    })
  },
  //查询当前信息 5S 更新一次
  getCurrent: function () {
    if (!this.data.refresh) return;
    wx.showNavigationBarLoading();
    let that = this;
    wx.request({
      url: getApp().globalData.host + 'listing/currentGroup',
      header: getApp().globalData.header,
      data: { 'groupId': that.data.groupId },
      method: 'POST',
      success: function (res) {
        if (typeof (res.data) != "undefined") {
          /**
          res.data.range = ((res.data.price - res.data.open)/res.data.open).toFixed(2);
          res.data.priceYuan = res.data.priceYuan.toFixed(2);
          res.data.rangeYuan = (res.data.priceYuan - res.data.openYuan).toFixed(2);
          **/
          that.setData({
            current: res.data
          })
        }
        wx.hideNavigationBarLoading();
      },
      fail: function (res) { },
      complete: function (res) {
      }
    })
    setTimeout(function(){
      that.getCurrent();
    },5000)
  },
  chooseKline: function (e) {
    let that = this;
    let kline = e.target.dataset.kline;
    this.data.canvasId = 2;
    this.data.period = 100;
    this.data.quoteData.canvasIndex = 1;
    if (kline == 'week') {
      this.data.canvasId = 3;
      this.data.period = 101;
      this.data.quoteData.canvasIndex = 2;
    }
    else if (kline == 'month') {
      this.data.canvasId = 4;
      this.data.period = 102;
      this.data.quoteData.canvasIndex = 3;
    }
    this.setData({
      kline: kline,
      canvasId: that.data.canvasId,
      period: that.data.period,
      quoteData: that.data.quoteData
    })
    this.kLineInit()
  },
  kLineInit: function () {
    let that = this;
    this.kLine = new kLine();
    let kline = this.data.kline
    this.kLine.isCanvasDrawn(this.data.canvasId);
    wx.request({
      url: getApp().globalData.host + 'listing/kLineData',
      header: getApp().globalData.header,
      data: { 'groupId': that.data.groupId, 'period': that.data.kline },
      method: 'POST',
      success: function (res) {
        if (typeof (res.data) != "undefined")
          //参数为，数据 | K线类型：1 分时；2 日线；3 周线；4 月线；5 小时线 | period 100日线；101 周线；102 月线
          that.kLine.drawKLineCanvas(res.data, that.data.canvasId, that.data.period);
        that.setData({
          hidden: true
        })
      },
      fail: function (res) { },
      complete: function (res) {
      }
    })
  },
  //查询挂牌批次列表
  getListings: function () {
    let that = this;
    wx.request({
      url: getApp().globalData.host + 'listing/list',
      header: getApp().globalData.header,
      data: { 'groupId': that.data.groupId },
      method: 'POST',
      success: function (res) {
        if (typeof (res.data.records) != "undefined") {
          for(let i=0;i<res.data.records.length;i++){
            res.data.records[i].price = (res.data.records[i].currentAmount/res.data.records[i].quantity).toFixed(2);
          }
          that.setData({
            listings: res.data.records
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {
      }
    })
  },
  //查看批次公告
  showNotice: function () {
    wx.navigateTo({
      url: '../notice/notice?groupId=' + this.data.groupId,
    })
  },
  //查看/购买具体批次
  getListingDetail: function (e) {
    var listingId = e.currentTarget.dataset.listingId;
    wx.navigateTo({
      url: '../listing/listing?listingId=' + listingId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      refresh: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      refresh: false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})